package com.wmzdq.photocenter.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.coobird.thumbnailator.Thumbnails;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wmzdq.photocenter.bean.FileBean;
import com.wmzdq.photocenter.bean.UploadItem;
import com.wmzdq.photocenter.common.PropertyUtil;
import com.wmzdq.photocenter.file.IFileOperator;

@RestController
@RequestMapping("/photo")
public class PhotoController {
	
	private Logger log=LoggerFactory.getLogger(PhotoController.class);
	
	@Autowired
	IFileOperator operator;
	
	@Autowired
	PropertyUtil proUtil;

	@RequestMapping(value="",method=RequestMethod.GET)
	public void quality(@RequestParam String path,HttpServletRequest request,
            HttpServletResponse response) throws IOException{
		String  img=operator.getLocal()+File.separator+path;
		
		response.setContentType("image/jpg");

        OutputStream stream = response.getOutputStream();
        Thumbnails.of(img).scale(1f) .outputQuality(0.25f) .outputFormat("jpg")  .toOutputStream(stream);  
        
       // stream.write(data);
        stream.flush();
        stream.close();
		
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody JSONObject upload (@RequestParam(value = "file_data") MultipartFile [] files,
	                 @RequestParam(value = "path") String path,
	                 HttpServletRequest request, HttpServletResponse response) {
		JSONObject result=new JSONObject();
		result.put("flag", true);
		ArrayList<FileBean> list=new ArrayList<FileBean>();
		String local=operator.getLocal();
		local=local+File.separator+path;
		try {
			if (files!=null) {
				for (MultipartFile file:files) {
					copyFile(file, local);
					FileBean bean=new FileBean();
					bean.setName(file.getOriginalFilename());
					bean.setUrl(path+File.separator+file.getOriginalFilename());
					bean.setThumpath(path+File.separator+operator.getThum()+File.separator+file.getOriginalFilename());
					list.add(bean);
				}
			}
	  } catch (Exception e) {
	    log.error("upload>>>>异常:{}", e.toString());
	  }
		
		result.put("items",list);
	  return result;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.DELETE)
	public @ResponseBody JSONObject  delete(@RequestParam(value="name") String filename,@RequestParam(value="path") String path){
		JSONObject result=new JSONObject();
		result.put("flag",true);
		
		operator.delete(filename,path);
		return result;
	}
	
	
	private FileBean copyFile (MultipartFile tempFile, String filePath) {
		Boolean res = false;
		FileBean item=new FileBean();
		String thum="";
		String filename=filePath+File.separator+tempFile.getOriginalFilename();
		item.setUrl(filename);
		item.setName(tempFile.getOriginalFilename());
		try {
			//File file = new File(filePath);
			/*if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}*/
			// 将文件拷贝到当前目录下
			InputStream is=tempFile.getInputStream();
			FileOutputStream fos=new FileOutputStream(filename);
			byte[] b = new byte[1024];
			while((is.read(b)) != -1){
				fos.write(b);
			}
			is.close();
			fos.close();
		     res = true;
		     
		     //生成缩略图
		     thum=operator.createThum(filePath, tempFile.getOriginalFilename());
		   } catch (Exception e) {
		     log.info("copyFile>>>>异常:{}", e.toString());
        }
		item.setThumpath(thum);
	   return item;
	}
}
